package com.mystyle.le.weekproject.fragment;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mystyle.le.weekproject.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 资讯页面
 * A simple {@link Fragment} subclass.
 */
public class GameFragment extends Fragment {


    private RadioGroup game_Rg;
    private ViewPager game_Vp;
    private String [] titles = {"头条","动漫","图片","段子"};
    private List<Fragment> list = new ArrayList<>();
    private RadioButton[] radioButtons ;

    public GameFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_game, container, false);
        //初始化控件
        initView(view);
        //初始化导航书签
        initTab();
        return view;
    }

    //初始化导航书签
    private void initTab() {
        radioButtons = new RadioButton[titles.length];
        for (int i =0;i<titles.length;i++){
            radioButtons[i] = new RadioButton(getActivity());
            radioButtons[i].setGravity(Gravity.CENTER);
            Bitmap a = null;
            radioButtons[i].setButtonDrawable(new BitmapDrawable(a));
            radioButtons[i].setText(titles[i]);
            radioButtons[i].setBackgroundResource(R.drawable.color_home);
            int screenWith = getResources().getDisplayMetrics().widthPixels;
            int eachWith = screenWith/titles.length;
            radioButtons[i].setWidth(eachWith);
            game_Rg.addView(radioButtons[i]);
        }
        radioButtons[0].setChecked(true);
        game_Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i =0;i<titles.length;i++){
                    if (radioButtons[i].getId()==checkedId){
                        game_Vp.setCurrentItem(i);
                    }
                }
            }
        });
    }

    //初始化控件
    private void initView(View view) {
        game_Rg = ((RadioGroup) view.findViewById(R.id.game_Rg));
        game_Vp = ((ViewPager) view.findViewById(R.id.game_Vp));
        HeadTitleFragment headTitleFragment = new HeadTitleFragment();//头条
        CartoonFragment cartoonFragment = new CartoonFragment();//动漫
        PictureFragment pictureFragment = new PictureFragment();//图片
        TalkFragment talkFragment = new TalkFragment();//段子
        list.add(headTitleFragment);
        list.add(cartoonFragment);
        list.add(pictureFragment);
        list.add(talkFragment);
        game_Vp.setAdapter(new MyGamePageAdapter(getChildFragmentManager()));
        game_Vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                radioButtons[position].setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyGamePageAdapter extends FragmentPagerAdapter{

        public MyGamePageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
